package com.profound.notes.main.slice;

import com.profound.notes.ResourceTable;
import com.profound.notes.ViewModelAbility;
import com.profound.notes.ViewModelSlice;
import com.profound.notes.main.slice.vm.MainPlaneViewModel;
import com.profound.notes.main.slice.vm.NoteViewModel;
import com.profound.notes.mvvm.ViewModelProvider;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.PageSlider;
import ohos.agp.components.TabList;

/**
 * @author zjwujlei
 * @date 2021/4/24
 * @email zjwujlei@sina.com
 * @description
 */
public class MainAbilitySlice extends ViewModelSlice implements TabList.TabSelectedListener, PageSlider.PageChangedListener, Component.ClickedListener {

    private Image mMore;
    private TabList mTab;
    private PageSlider mSlider;
    private ViewModelProvider mProvider;
    private MainPlaneViewModel mPlaneVM;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        initVM();
        initView();

        try {
            Class clazz = Class.forName("android.support.v7.widget.RecyclerView");
            System.out.println("1111111RecyclerView");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            System.out.println("22222222RecyclerView");
        }
    }

    private void initVM(){
        mProvider = new ViewModelProvider(this,new ViewModelProvider.AbilityViewModelFactory(getAbility()));
        mProvider.get(NoteViewModel.class).fetchNotes();
        mPlaneVM = mProvider.get(MainPlaneViewModel.class);
    }
    private void initView(){
        mMore = (Image) findComponentById(ResourceTable.Id_icon_more);
        mTab = (TabList) findComponentById(ResourceTable.Id_tab_list);
        mSlider = (PageSlider) findComponentById(ResourceTable.Id_main_plane);
        TabList.Tab noteTab = mTab.new Tab(MainAbilitySlice.this);
        noteTab.setText("便签");
        noteTab.setTag(0);
        mTab.addTab(noteTab);
        TabList.Tab toDoTab = mTab.new Tab(MainAbilitySlice.this);
        toDoTab.setText("待办");
        toDoTab.setTag(1);
        mTab.addTab(toDoTab);
        noteTab.select();
        mTab.addTabSelectedListener(this);

        mSlider.setProvider(new PlanePageSliderProvider(mProvider,this));
        mSlider.addPageChangedListener(this);
        mMore.setClickedListener(this);
    }


    @Override
    public void onActive() {
        super.onActive();

    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    //Tab相关回调
    @Override
    public void onSelected(TabList.Tab tab) {
        int tag = (int) tab.getTag();
        if (tag == 0){
            mPlaneVM.planeAction(MainPlaneViewModel.PLANE_NOTE);
        }else if (tag == 1){
            mPlaneVM.planeAction(MainPlaneViewModel.PLANE_TODO);
        }

        mSlider.setCurrentPage(tag);

    }

    @Override
    public void onUnselected(TabList.Tab tab) {

    }

    @Override
    public void onReselected(TabList.Tab tab) {

    }

    //Slider相关回调
    @Override
    public void onPageSliding(int i, float v, int i1) {

    }

    @Override
    public void onPageSlideStateChanged(int i) {

    }

    @Override
    public void onPageChosen(int i) {
        if (i == 0){
            mPlaneVM.planeAction(MainPlaneViewModel.PLANE_NOTE);
        }else if (i == 1){
            mPlaneVM.planeAction(MainPlaneViewModel.PLANE_TODO);
        }

        mTab.selectTabAt(i);
    }

    //点击事件
    @Override
    public void onClick(Component component) {

    }
}
